package com.swx.generate.util;

import com.aspose.words.Document;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Slf4j
public class ConfigUtil {

    private static String jmxTemplate = "";

    private static Document interfaceHeaderTemplateDoc;
    private static Document interfaceBodyTemplateDoc;
    private static Document testcaseHeaderTemplateDoc;
    private static Document testcaseBodyTemplateDoc;
    private static Document xxsjInterfaceBodyTemplateDoc;

    public static String getJmxTemplate() {
        if (StringUtils.isEmpty(jmxTemplate)) {
            ClassPathResource classPathResource = new ClassPathResource("templates\\jmeter\\jmx-template.xml");
            try (InputStream inputStream = classPathResource.getInputStream()) {
                jmxTemplate = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
                log.info("读取jmx模板成功");
            } catch (IOException e) {
                e.printStackTrace();
                log.error("读取jmx模板失败", e);
            }
        }
        return jmxTemplate;
    }

    public static Document getInterfaceHeaderTemplateDoc() {
        if (interfaceHeaderTemplateDoc == null) {

            ClassPathResource classPathResourceHeader = new ClassPathResource("templates\\word\\interface\\doc-header.doc");
            try (InputStream resourceAsStreamHeader = classPathResourceHeader.getInputStream()) {
                interfaceHeaderTemplateDoc = new Document(resourceAsStreamHeader);
                log.info("读取接口设计模板头成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("读取接口设计模板头失败", e);
            }
        }
        return interfaceHeaderTemplateDoc;
    }

    public static Document getInterfaceBodyTemplateDoc() {
        if (interfaceBodyTemplateDoc == null) {
            ClassPathResource classPathResourceInterface = new ClassPathResource("templates\\word\\interface\\doc-body.doc");
            try (InputStream resourceAsStreamInterface = classPathResourceInterface.getInputStream()) {
                interfaceBodyTemplateDoc = new Document(resourceAsStreamInterface);
                log.info("读取接口设计模板成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("读取接口设计模板失败", e);
            }
        }
        return interfaceBodyTemplateDoc;
    }

    public static Document getTestcaseHeaderTemplateDoc() {
        if (testcaseHeaderTemplateDoc == null) {
            ClassPathResource classPathResourceInterface = new ClassPathResource("templates\\word\\test-case\\doc-header.doc");
            try (InputStream resourceAsStreamInterface = classPathResourceInterface.getInputStream()) {
                testcaseHeaderTemplateDoc = new Document(resourceAsStreamInterface);
                log.info("读取测试用例模板头成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("读取测试用例模板头失败", e);
            }
        }
        return testcaseHeaderTemplateDoc;
    }

    public static Document getTestcaseBodyTemplateDoc() {
        if (testcaseBodyTemplateDoc == null) {
            ClassPathResource classPathResourceInterface = new ClassPathResource("templates\\word\\test-case\\doc-body.doc");
            try (InputStream resourceAsStreamInterface = classPathResourceInterface.getInputStream()) {
                testcaseBodyTemplateDoc = new Document(resourceAsStreamInterface);
                log.info("读取测试用例模板成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("读取测试用例模板失败", e);
            }
        }
        return testcaseBodyTemplateDoc;
    }

    public static Document getXxsjInterfaceBodyTemplateDoc() {
        if (xxsjInterfaceBodyTemplateDoc == null) {
            ClassPathResource classPathResourceInterface = new ClassPathResource("templates\\word\\xxsj-interface\\doc-body.doc");
            try (InputStream resourceAsStreamInterface = classPathResourceInterface.getInputStream()) {
                xxsjInterfaceBodyTemplateDoc = new Document(resourceAsStreamInterface);
                log.info("读取详细设计接口模板成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("读取详细设计接口模板失败", e);
            }
        }
        return xxsjInterfaceBodyTemplateDoc;
    }
}
